package com.herui.ota.controller;

import cn.hutool.core.bean.BeanUtil;
import com.herui.ota.config.ControllerNames;
import com.herui.ota.pojo.form.NoneFilterForm;
import com.mlnx.common.annotations.NeedLogin;
import com.mlnx.common.entity.Response;
import com.mlnx.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.shan.base.layer.user.entity.Menu;
import org.shan.base.layer.user.entity.Role;
import org.shan.base.layer.user.pojo.form.RoleForm;
import org.shan.base.layer.user.service.RoleService;
import org.shan.spring.base.CurdBaseController;
import org.shan.spring.base.RdBaseController;
import org.shan.spring.base.annotations.SysLog;
import org.shan.spring.mybatis.service.ICurdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * Created by amanda.shan on 2020/5/19.
 */
@Api(value = ControllerNames.RoleName + "接口", tags = {ControllerNames.RoleName + "接口"})
@SysLog(title = ControllerNames.RoleName)
@RestController
@RequestMapping("/role")
public class RoleController extends CurdBaseController<Role, Role, Role, NoneFilterForm> {

    @Autowired
    private RoleService roleService;

    @Override
    public ICurdService getService() {
        return roleService;
    }

    @Override
    public Role newInstance(Integer id) {
        Role role = new Role();
        role.setId(id);
        return role;
    }

    @NeedLogin(permissions={"role_edit"})
    @SysLog(
            businessType = BusinessType.UPDATE
    )
    @ApiOperation(
            value = "更改",
            notes = ""
    )
    @PutMapping({"/menu/{id}"})
    public Response modify(@PathVariable("id") Integer id, @Valid @RequestBody List<Menu> menus) {
        roleService.bindMenu(id, menus);
        return this.result();
    }

    @NeedLogin(permissions={"role_edit"})
    @SysLog(
            businessType = BusinessType.INSERT
    )
    @ApiOperation(
            value = "新增",
            notes = ""
    )
    @PostMapping({""})
    public Response add(@Valid Role form) throws IllegalAccessException, InstantiationException {
        Role role = new Role();
        BeanUtil.copyProperties(form, role);
        this.getService().save(role);
        return this.result();
    }

    @NeedLogin(permissions={"role_edit"})
    @SysLog(
            businessType = BusinessType.DELETE
    )
    @ApiOperation(
            value = "删除",
            notes = ""
    )
    @DeleteMapping({"/{id}"})
    public Response delete(@PathVariable("id") Integer id) {
        this.getService().removeById(id);
        return this.result();
    }

    @NeedLogin(permissions={"role_edit"})
    @SysLog(
            businessType = BusinessType.DELETE
    )
    @ApiOperation(
            value = "批量删除",
            notes = ""
    )
    @DeleteMapping({""})
    public Response deletes(Integer[] ids) {
        this.getService().removeByIds(Arrays.asList(ids));
        return this.result();
    }

}
